//
//  TAMTCreateGroupController.swift
//  TAMeetApp
//
//  Created by 九月 on 12/5/24.
//

import UIKit
import SVProgressHUD

class TAMTCreateGroupController: UIViewController, UIScrollViewDelegate, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    var coverImg:UIImage?
    

    @IBOutlet weak var subView1: UIView!
    
    @IBOutlet weak var coverView: UIImageView!
    @IBOutlet weak var tagBtn1: UIButton!
    @IBOutlet weak var tagBtn2: UIButton!
    @IBOutlet weak var tagBtn3: UIButton!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var textbgView: UIView!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var submitBtn: UIButton!
    @IBOutlet weak var placeLabel: UILabel!
    @IBOutlet weak var bottomLabel1: UILabel!
    @IBOutlet weak var bottomLabel2: UILabel!
    @IBOutlet weak var bottomLabel3: UILabel!
    @IBOutlet weak var bottomLabel4: UILabel!
    
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var nameTF: UITextField!
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "创建他遇群聊"
        
        self.subView1.backgroundColor = .white
        subView1.layer.cornerRadius = 16
        subView1.layer.masksToBounds = true
        
        
        self.coverView.layer.cornerRadius = 12
        self.coverView.layer.masksToBounds = true
        
        tagBtn1.setTitle("兴趣爱好", for: .normal)
        tagBtn1.layer.cornerRadius = 14
        tagBtn1.layer.masksToBounds = true
        
        tagBtn2.setTitle("专业技术", for: .normal)
        tagBtn2.layer.cornerRadius = 14
        tagBtn2.layer.masksToBounds = true
        
        tagBtn3.setTitle("聊天交友", for: .normal)
        tagBtn3.layer.cornerRadius = 14
        tagBtn3.layer.masksToBounds = true
        
        textbgView.layer.cornerRadius = 14
        textbgView.layer.masksToBounds = true
        
        
        submitBtn.layer.cornerRadius = 26
        submitBtn.layer.masksToBounds = true
        

        scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 52 + TAMTAppUtils.shared.safeAreaInsets.bottom + 20, right: 0)
        scrollView.delegate = self
        
        label1.text = "设置定位，方便认识更想认识的Ta"
        bottomLabel1.text = "注意事项"
        bottomLabel2.text = "1. 群名称，每个月最多修改1次，且不能重名"
        bottomLabel3.text = "2. 创建群需要先认证，同时粉丝量达到100及以上，若不达标，则需要消耗199他遇币"
        bottomLabel4.text = "3. 请遵守规则，绿色聊天交友"
        placeLabel.text = "在这里输入群里相关的介绍吧"
        
        nameTF.placeholder = "设置一个响亮霸气的名称"
        
        textView.delegate = self
        
        tagBtn1.isSelected = true
        
        tagBtn1.setBackgroundImage(UIColor.init(hex: "#E5EDFF").toImage(size: CGSize(width: 80, height: 32)), for: .normal)
        tagBtn1.setBackgroundImage(UIColor.init(hex: "#578AFE").toImage(size: CGSize(width: 80, height: 32)), for: .selected)
        tagBtn1.setTitleColor(.white, for: .selected)
        
        tagBtn2.setBackgroundImage(UIColor.init(hex: "#E5EDFF").toImage(size: CGSize(width: 80, height: 32)), for: .normal)
        tagBtn2.setBackgroundImage(UIColor.init(hex: "#578AFE").toImage(size: CGSize(width: 80, height: 32)), for: .selected)
        tagBtn2.setTitleColor(.white, for: .selected)
        
        tagBtn3.setBackgroundImage(UIColor.init(hex: "#E5EDFF").toImage(size: CGSize(width: 80, height: 32)), for: .normal)
        tagBtn3.setBackgroundImage(UIColor.init(hex: "#578AFE").toImage(size: CGSize(width: 80, height: 32)), for: .selected)
        tagBtn3.setTitleColor(.white, for: .selected)
        
        submitBtn.isEnabled = false
        submitBtn.setBackgroundImage(TAMTAppUtils.shared.mainColor.toImage(size: CGSize(width: 80, height: 32)), for: .normal)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.view.endEditing(true)
    }
    
    @IBAction func onTagAction(_ sender: UIButton) {
        tagBtn1.isSelected = sender == tagBtn1
        tagBtn2.isSelected = sender == tagBtn2
        tagBtn3.isSelected = sender == tagBtn3
    }
    
    @IBAction func textFeildChangge(_ sender: UITextField) {
        submitStatus()
    }
    
    @IBAction func onSubmitAction() {
        showKnonw()
    }
    
    @IBAction func onChoseImageAction(_ sender: UITapGestureRecognizer) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary // 从相册选择
        imagePicker.mediaTypes = ["public.image"] // 指定媒体类型为图片

        present(imagePicker, animated: true, completion: nil)
    }
    
    func textViewDidChange(_ textView: UITextView) {
        placeLabel.isHidden = textView.text.count>0
        submitStatus()
    }
    
    func submitStatus() {
        let count = nameTF.text?.count ?? 0
        self.submitBtn.isEnabled = textView.text.count>0 && count>0 && coverImg != nil
    }

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        // 阻止换行符的输入
        if text == "\n" {
            textView.resignFirstResponder()  // 结束编辑
            return false
        }
        return true
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.originalImage] as? UIImage else { return }
        coverImg = image
        coverView.image = image
        submitStatus()
        dismiss(animated: true, completion: nil)
    }
    
    func showKnonw() {
        let bottomView = UIView()
        bottomView.size = CGSize(width: 320, height: 220)
        bottomView.backgroundColor = .white
        bottomView.layer.cornerRadius = 16
        bottomView.layer.masksToBounds = true
        
        let topLabel = UILabel()
        topLabel.text = "温馨提示"
        topLabel.textColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1)
        topLabel.font = .systemFont(ofSize: 16, weight: .medium)
        bottomView.addSubview(topLabel)
        topLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
        }
        
        let view1 = UIView()
        view1.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1)
        bottomView.addSubview(view1)
        view1.snp.makeConstraints { make in
            make.bottom.centerX.equalToSuperview()
            make.width.equalTo(1)
            make.height.equalTo(55)
        }
        
        let view2 = UIView()
        view2.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1)
        bottomView.addSubview(view2)
        view2.snp.makeConstraints { make in
            make.bottom.equalTo(view1.snp.top)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let str:String = "你当前拥有9个粉丝，未达到创建条件，若要继续创建，则需要支付99他遇币, 是否继续创建"
        
        
        let topLabel1 = UILabel()
        topLabel1.attributedText = str.attributedStringWithlineSpacing(5.0, textColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1), font: UIFont.systemFont(ofSize: 16, weight: .regular))
        topLabel1.numberOfLines = 0
        topLabel1.textAlignment = .center
        bottomView.addSubview(topLabel1)
        topLabel1.snp.makeConstraints { make in
            make.leading.equalTo(20)
            make.trailing.equalTo(-20)
            make.top.equalTo(topLabel.snp.bottom).offset(20)
        }
        
        
        let leftBtn = UIButton(type: .custom)
        leftBtn.setTitle("去创建", for: .normal)
        leftBtn.setTitleColor(TAMTAppUtils.shared.mainColor, for: .normal)
        leftBtn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
        leftBtn.addTarget(self, action: #selector(onCreareAction), for: .touchUpInside)
        bottomView.addSubview(leftBtn)
        leftBtn.snp.makeConstraints { make in
            make.trailing.bottom.equalToSuperview()
            make.height.equalTo(54)
            make.left.equalTo(view1.snp.right)
        }
        
        let leftBtn1 = UIButton(type: .custom)
        leftBtn1.setTitle("我知道了", for: .normal)
        leftBtn1.setTitleColor(UIColor(red: 184/255.0, green: 187/255.0, blue: 190/255.0, alpha: 1), for: .normal)
        leftBtn1.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
        leftBtn1.addTarget(self, action: #selector(onHidenAction), for: .touchUpInside)
        bottomView.addSubview(leftBtn1)
        leftBtn1.snp.makeConstraints { make in
            make.leading.bottom.equalToSuperview()
            make.height.equalTo(54)
            make.right.equalTo(view1.snp.left)
        }
        
        let popup = APPopupView(contentView: bottomView, position: .center, contentWidth: bottomView.width, contentHeight: bottomView.height)
        popup.show()
    }
    
    @objc func onCreareAction() {
        APPopupView.hidden { [weak self] in
            SVProgressHUD.show()
            DispatchQueue.global().asyncAfter(deadline: .now() + 2) { [weak self] in
                SVProgressHUD.showError(withStatus: "上传图片失败，请稍后再试")
            }
        }
    }
    
    @objc func onHidenAction() {
        APPopupView.hidden()
    }
}
